/**
 * 
 */
package com.lgx.right.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import net.sf.json.JSONObject;

import com.lgx.common.entity.IdGenerator;


/**
 * @author hezhuo
 * 
 * 用户角色
 *
 */

@Entity
@Table(name = "TS_ROLE")
public class Role implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5452616929081810236L;

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator=IdGenerator.TS_ROLE_ID)
	@TableGenerator(
			name = IdGenerator.TS_ROLE_ID,
			table = IdGenerator.GENERATOR_TABLE,
			pkColumnName = IdGenerator.GEN_NAME,
			pkColumnValue = IdGenerator.TS_ROLE_ID,
			valueColumnName = IdGenerator.GEN_VALUE,
			initialValue = 1,
			allocationSize = 1
	)
	private Long id;
	
	/**
	 * 角色名称
	 */
	private String name;
	
	/**
	 * 角色对应权限ID
	 */
	private String mids;
	
	private String pns;
	
	/**
	 * 角色创建人
	 */
	@Column(name="creater_id")
	private long createrId;
	
	private String createName;
	
	/**
	 * 角色创建时间
	 */
	private Date createTime;
	
	@ManyToMany(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER)    
    @JoinTable(name="TS_ROLE_PERMISSION",   
             joinColumns=@JoinColumn(name="ROLE_ID"),   
             inverseJoinColumns=@JoinColumn(name="PERMISSION_ID")) 
	private Set<Permission> permissions;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMids() {
		return mids;
	}

	public void setMids(String mids) {
		this.mids = mids;
	}

	public long getCreaterId() {
		return createrId;
	}

	public void setCreaterId(long createrId) {
		this.createrId = createrId;
	}

	public String getCreateName() {
		return createName;
	}

	public void setCreateName(String createName) {
		this.createName = createName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Set<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	public String getPns() {
		return pns;
	}

	public void setPns(String pns) {
		this.pns = pns;
	}

	@Override
	public String toString() {
		JSONObject obj = new JSONObject();
		
		obj.put("id", this.getId());
		obj.put("name", this.getName());
		obj.put("permissionNames", pns);
		obj.put("mids", mids);
		
		return obj.toString();
	}
}
